/*
 * This program is free software; you can redistribute it and/or modify it under the 
 * terms of the GNU Lesser General Public License, version 2.1 as published by the Free Software 
 * Foundation.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this 
 * program; if not, you can obtain a copy at http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html 
 * or from the Free Software Foundation, Inc., 
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * Copyright 2008 Pentaho Corporation.  All rights reserved.
 *
 * @author Will Gorman (wgorman@pentaho.com)
 */
package org.pentaho.actioneditor.client.processpanels;

import org.pentaho.actioneditor.client.util.EventRichFlexTable;
import org.pentaho.actionsequence.dom.IActionSequenceOutput;
import org.pentaho.actionsequence.dom.IActionSequenceOutputDestination;

import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.FocusListener;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.SourcesTableEvents;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class OutputPanel {
  final OutputUI oui = new OutputUI();
  IActionSequenceOutput output;
  
  public OutputPanel(IActionSequenceOutput output) {
    this.output = output;
  }
  
  public Widget getRoot() {
    return oui.getRoot();
  }
  
  public void init() {
   
    oui.setAddDestinationCommand(new OutputUI.ItemCommand() {
      public void execute(String text) {
        output.addDestination(text, output.getName());
        renderDestinationTable();
      }
    });

    // oui.getRoot().setBorderWidth(1);
    oui.init();
    
    oui.getNameLabel().setText(output.getName());
    
    oui.getOutputTypeLabel().setText(output.getType());
    
    oui.getDeleteDestination().setCommand(new Command() {
      public void execute() {
        if (selectedDestinationRow != -1 && output.getDestinations().length > selectedDestinationRow) {
          output.getDestinations()[selectedDestinationRow].delete();
          // re-render table
          renderDestinationTable();
        }
      }
    });
    
    renderDestinationTable();
  }
  
  int selectedDestinationRow;
  EventRichFlexTable destinationTable;
  
  private void renderDestinationTable() {
    IActionSequenceOutputDestination destinations[] = output.getDestinations();
    selectedDestinationRow = -1;

   oui.getTableScroller().clear();
   destinationTable = new EventRichFlexTable();
   destinationTable.setWidth("100%");
   

   destinationTable.setWidget(0, 0, new Label("Destination"));
   destinationTable.setWidget(0, 1, new Label("Name"));
   destinationTable.getRowFormatter().setStylePrimaryName(0, "gwt-HeaderRow");
    
    for (int i = 0; i < destinations.length; i++) {
      destinationTable.getRowFormatter().setStylePrimaryName(i+1, "gwt-DataRow" + ((i % 2 == 0) ? "Even" : "Odd"));
      destinationTable.setWidget(i + 1, 0, new Label(destinations[i].getDestination()));
      destinationTable.setWidget(i + 1, 1, new Label(destinations[i].getName()));
    }    
    
    oui.getTableScroller().setWidget(destinationTable);
    
    destinationTable.addTableListener(new EventRichFlexTable.EventRichTableListener() {
      public void onCellClicked(SourcesTableEvents sender, int row, int cell) {
        System.out.println("CLICKED : " + row + ", " + cell + ", style=" + destinationTable.getRowFormatter().getStyleName(row) + ", " + destinationTable.getRowFormatter().getStylePrimaryName(row));
        if (row != 0) {
          setSelectedDestinationRow(row - 1);
        }
      }

      public void onCellDblClicked(SourcesTableEvents sender, int row, int cell) {
        System.out.println("DOUBLE CLICKED : " + row + ", " + cell + ", style=" + destinationTable.getRowFormatter().getStyleName(row) + ", " + destinationTable.getRowFormatter().getStylePrimaryName(row));
        if (selectedDestinationRow != -1) {
          destinationTable.setWidget(selectedDestinationRow + 1, 0, new Label(output.getDestinations()[selectedDestinationRow].getDestination()));
          destinationTable.setWidget(selectedDestinationRow + 1, 1, new Label(output.getDestinations()[selectedDestinationRow].getName()));
        }
        if (row != 0) {
          setSelectedDestinationRow(row - 1);
          if (cell == 0) {
            
            System.out.println("RENDER DROPDOWN");
            final ListBox listbox = new ListBox();
            listbox.addItem("runtime");
            listbox.addItem("session");
            listbox.addItem("response");
            listbox.addItem("global");
            listbox.addItem("file");
            listbox.addItem("vfs-ftp");
            
            listbox.addFocusListener(new FocusListener() {

              public void onFocus(Widget sender) {
                System.out.println("LIST BOX GAINED FOCUS");
              }

              public void onLostFocus(Widget sender) {
                // get the values of the pulldown or text box
                System.out.println("LIST FOCUS LOST: " + listbox.getItemText(listbox.getSelectedIndex()));
                output.getDestinations()[selectedDestinationRow].setDestination(listbox.getItemText(listbox.getSelectedIndex()));
                destinationTable.setWidget(selectedDestinationRow + 1, 0, new Label(output.getDestinations()[selectedDestinationRow].getDestination()));
                destinationTable.setWidget(selectedDestinationRow + 1, 1, new Label(output.getDestinations()[selectedDestinationRow].getName()));
              }
              
            });
            
            for (int i = 0; i < listbox.getItemCount(); i++) {
              String text = listbox.getItemText(i);
              if (text.equals(output.getDestinations()[row - 1].getDestination())) {
                listbox.setSelectedIndex(i);
                break;
              }
            }
            
            listbox.addChangeListener(new ChangeListener() {
              public void onChange(Widget sender) {
                output.getDestinations()[selectedDestinationRow].setDestination(listbox.getItemText(listbox.getSelectedIndex()));
              }
            });

            // TODO: NEED TO CLEAR OUT DROP DOWN ONCE FOCUS IS LOST
            
            destinationTable.setWidget(row, cell, listbox);
            listbox.setFocus(true);
            // selectedSourceCell = cell;
          } else if (cell == 1) {
            final TextBox text = new TextBox();
            text.setText(output.getDestinations()[selectedDestinationRow].getName());
            text.addChangeListener(new ChangeListener() {
              public void onChange(Widget sender) {
                System.out.println("Text Changed : " + text.getText());
                output.getDestinations()[selectedDestinationRow].setName(text.getText());
              }
            });
            
            text.addFocusListener(new FocusListener() {
              public void onFocus(Widget sender) {
                System.out.println("TEXT BOX FOCUS");
              }

              public void onLostFocus(Widget sender) {
                System.out.println("Text Focus Lost : " + text.getText());
                output.getDestinations()[selectedDestinationRow].setName(text.getText());
                destinationTable.setWidget(selectedDestinationRow + 1, 0, new Label(output.getDestinations()[selectedDestinationRow].getDestination()));
                destinationTable.setWidget(selectedDestinationRow + 1, 1, new Label(output.getDestinations()[selectedDestinationRow].getName()));
              }
            });
            System.out.println("RENDER TEXTBOX");
            destinationTable.setWidget(row, cell, text);
            text.setFocus(true);
            // selectedSourceCell = cell;
          }
        }
        
      }
    });

  }
  
  public void setSelectedDestinationRow(int row) {
    System.out.println("SET SELECTED DEST ROW: " + row + " (was " + selectedDestinationRow + ")");
    if (selectedDestinationRow == row) {
      return;
    }
    if (selectedDestinationRow != -1) {
      // clear out any widgets
      destinationTable.setWidget(selectedDestinationRow + 1, 0, new Label(output.getDestinations()[selectedDestinationRow].getDestination()));
      destinationTable.setWidget(selectedDestinationRow + 1, 1, new Label(output.getDestinations()[selectedDestinationRow].getName()));
      destinationTable.getRowFormatter().setStyleName(selectedDestinationRow + 1, "gwt-DataRow" + ((selectedDestinationRow % 2 == 0) ? "Even" : "Odd"));
    }
    selectedDestinationRow = row;
    destinationTable.getRowFormatter().setStyleName(row + 1, "gwt-DataRowSelected");
  }
}
